//===================================================================\\
//                                           Mod_Trendy_MTF_1-02.mq4 ||
//                      modification_Nondisclosure007 (by Im_hungree)||
//                                 Copyright ?31.01.2012, Alexander ||
//   email:I-m-hungree@yandex.ru | skype: Im_hungree | ICQ:609928564 ||
//===================================================================//
#property copyright           "Copyright ?31.01.2012, Alexander"
#property link                "Im_hungree"
#property indicator_separate_window
#property indicator_buffers	2
#property indicator_color1		Blue
#property indicator_color2		Red
#property indicator_level1		75
#property indicator_level2		60
#property indicator_maximum	100
#property indicator_minimum	0

//===================================================================//
//---- input parameters
extern int	       TimeFrame           = 0;		            // {0 = Current TF, 1=M1, 5=M5, 15=M15, ..., 1440=D1, 10080=W1, 43200=MN1}
extern string      TimeFrame_Help = "0 = Current TF, 1=M1, 5=M5, 15=M15, ..., 1440=D1, 10080=W1, 43200=MN1";
extern color       Dns                 = Yellow;            // color arrow down
extern color       Ups                 = Red;               // color arrow Up
extern bool Text.Alert = false;
extern bool Sound.Alert = false;
extern bool Email.Alert = false;
extern string      signal_buy          = "alert";           // a signal to buy
extern string      signal_sell         = "alert2";          // a signal to sell
//===================================================================//
//---- indicator buffers

double		Buffer1[],
				Buffer2[];
int         nomber;
datetime lastTime;

//===================================================================//
//          Custom indicator initialization function                 ||
//===================================================================//
int init()
{
//---- indicator lines
  SetIndexBuffer(0,Buffer1);
  SetIndexBuffer(1,Buffer2);
  SetIndexStyle(0,DRAW_LINE);
  SetIndexStyle(1,DRAW_LINE);
  SetIndexLabel(0,"Up");
  SetIndexLabel(1,"Down");

//---- name for DataWindow and indicator subwindow label

  string TimeFrameStr;
  switch(TimeFrame)
	{
		case 1:		TimeFrameStr="Period M1"; break;
		case 5:		TimeFrameStr="Period M5"; break;
		case 15:		TimeFrameStr="Period M15"; break;
		case 30:		TimeFrameStr="Period M30"; break;
		case 60:		TimeFrameStr="Period H1"; break;
		case 240:	TimeFrameStr="Period H4"; break;
		case 1440:	TimeFrameStr="Period D1"; break;
		case 10080:	TimeFrameStr="Period W1"; break;
		case 43200:	TimeFrameStr="Period MN1"; break;
		default:		TimeFrameStr="Current Timeframe";
	}
  IndicatorShortName(TimeFrameStr+" Trendy");  
  
   for(int ui = ObjectsTotal(OBJ_ARROW) - 1; ui >= 0; ui--)
     {
      string name2 = ObjectName(ui);
       if(StringFind(name2, "prefics", 0) != -1)
         ObjectDelete(""+name2+"");   
     }
}
//===================================================================//
//         Custom indicator deinitialization function                ||
//===================================================================//
int deinit()
{
//----
  for(int ui = ObjectsTotal(OBJ_ARROW) - 1; ui >= 0; ui--)
   {
    string name1 = ObjectName(ui);
      if(StringFind(name1, "prefics", 0) != -1)
        ObjectDelete(""+name1+"");   
   }
//----
  return(0);
}
//===================================================================//
//                        MTF Stochastic                             ||
//===================================================================//
int start()
{

//===========================
   if (!isNewBar()) return;  
   if (TimeFrame == 0) TimeFrame = Period();

	for(int i = Bars-1-IndicatorCounted(); i >= 0; i--)
	{
		int shift1 = iBarShift(NULL,TimeFrame,Time[i]),
			 time1  = iTime    (NULL,TimeFrame,shift1),
			 shift2 = iBarShift(NULL,0,time1);

		Buffer1[shift2] = iCustom(NULL,TimeFrame,"Trendy",0,shift1);
		Buffer2[shift2] = iCustom(NULL,TimeFrame,"Trendy",1,shift1);
      //Print ("---1---,,Buffer1[i+2]= ",Buffer1[i+2],"Buffer2[i+2]= ",Buffer2[i+2],"Buffer1[i+1]= ",Buffer1[i+1],"Buffer2[i+1]= ",Buffer2[i+1]);
//----
//	linear interpolation for indicators from a higher timeframe
		if(TimeFrame <= Period())
			continue;
//----
//	current candle
		if(shift1==0)
			for(int n = 1; shift2-n >= 0; n++)
			{
				Buffer1[shift2-n] = Buffer1[shift2];
				Buffer2[shift2-n] = Buffer2[shift2];
      //Print ("---2---,,Buffer1[i+2]= ",Buffer1[i+2],"Buffer2[i+2]= ",Buffer2[i+2],"Buffer1[i+1]= ",Buffer1[i+1],"Buffer2[i+1]= ",Buffer2[i+1]);
			}
//	count number of intermediate bars
		for(n = 1; shift2+n < Bars && Time[shift2+n] > iTime(NULL,TimeFrame,iBarShift(NULL,TimeFrame,Time[shift2+n],true)); n++)
			continue;
//----
//	apply interpolation
		double factor = 1.0 / n;
		for(int k = 1; k < n; k++)
		{
			Buffer1[shift2+k] = k*factor*Buffer1[shift2+n] + (1.0-k*factor)*Buffer1[shift2];
			Buffer2[shift2+k] = k*factor*Buffer2[shift2+n] + (1.0-k*factor)*Buffer2[shift2];
      //Print ("---3---,,Buffer1[i+2]= ",Buffer1[i+2],"Buffer2[i+2]= ",Buffer2[i+2],"Buffer1[i+1]= ",Buffer1[i+1],"Buffer2[i+1]= ",Buffer2[i+1]);
		}
		
		
		
	}
	
	for(i = Bars-1-IndicatorCounted(); i >= 0; i--)
	{
	     if (Buffer1[i+2] != 2147483647 && Buffer2[i+2] != 2147483647 &&
          Buffer1[i+1] != 2147483647 && Buffer2[i+1] != 2147483647)
        {
         //Print ("1,,i= ",i,"nomber= ",nomber," Time= ",TimeToStr(Time[it],TIME_DATE|TIME_MINUTES));
         //Print ("---4---,,Buffer1[it+2]= ",Buffer1[it+2],"Buffer2[it+2]= ",Buffer2[it+2],"Buffer1[it+1]= ",
                // Buffer1[it+1],"Buffer2[it+1]= ",Buffer2[it+1]);
         if (Buffer1[i+2] >= Buffer2[i+2] && Buffer1[i+1] < Buffer2[i+1])
           {
             ObjectCreate("" + nomber + "prefics",OBJ_ARROW,0,Time[i+1],High[i+1]);
             ObjectSet("" + nomber + "prefics",OBJPROP_ARROWCODE,234);
             ObjectSet("" + nomber + "prefics",OBJPROP_COLOR,Dns);
             nomber     += 1;

             string alertStr = "Pair:" + Symbol() + ", T/F:" +  getTimeFrame(TimeFrame) + ", Direction: SHORT, Price:" + DoubleToStr(Bid, Digits);                   
               if (Text.Alert && i == 0)
               {
                   Alert(alertStr);
                   Print(alertStr);
               }
               if (Email.Alert && i == 0)
               {
                   SendMail("Trendy SHORT Alert" , alertStr);  
               }
               if (Sound.Alert && i == 0)
               {
                   PlaySound(signal_sell);
               }      
           }
         if (Buffer1[i+2] <= Buffer2[i+2] && Buffer1[i+1] > Buffer2[i+1])
           {
             ObjectCreate("" + nomber + "prefics",OBJ_ARROW,0,Time[i+1],Low[i+1]);
             ObjectSet("" + nomber + "prefics",OBJPROP_ARROWCODE,233);
             ObjectSet("" + nomber + "prefics",OBJPROP_COLOR,Ups);
             nomber     += 1;
             
              alertStr = "Pair:" + Symbol() + ", T/F:" +  getTimeFrame(TimeFrame) + ", Direction: LONG, Price:" + DoubleToStr(Ask, Digits);
               
              if (Text.Alert && i == 0)
               {
                   Alert(alertStr);
                   Print(alertStr);
               }
               if (Email.Alert && i == 0)
               {
                   SendMail("Trendy LONG Alert" , alertStr);  
               }
               if (Sound.Alert && i == 0)
               {
                   PlaySound(signal_buy);
               }   
           }
        }
   }
//
//==============================

	return(0);
}

bool isNewBar()
{
    if (lastTime == Time[0]) return (false);   
    lastTime = Time[0];
    return (true); 
}

string getTimeFrame(int _Period)
{
    switch (_Period)
    {
        case 1: return ("M1");
                break;
        case 5: return ("M5");
                break;
        case 15: return ("M15");
                break;
        case 30: return ("M30");
                break;
        case 60: return ("H1");
                break;
        case 240: return ("H4");
                break;
        case 1440: return ("D1");
                break;
        case 10080: return ("W1");
                break;
        case 43200: return ("Month");
                break;
    }
}




